package br.com.poliwar.view {
	import br.com.poliwar.model.vo.UsuarioVO;
	import br.com.poliwar.view.events.TelaCadastroEvent;
	import flash.events.MouseEvent;
	/**
	 * ...
	 * @author Ricardo Teixeira
	 */
	public class TelaCadastro extends CadastroSWC{
				
		public function TelaCadastro() {			
			this.campoSenha.displayAsPassword = true;
			this.campoConfirmaSenha.displayAsPassword = true;
			this.btCadastrar.addEventListener (MouseEvent.CLICK, onClickCadastrar);
		}
		
		private function validarEmail (email:String):Boolean {
			var emailExpression:RegExp = /^[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i;
			return emailExpression.test (email);
		}
		
		private function validarCadastro ():Boolean {
			var senha:Boolean = (this.campoSenha.text.length >= 3) && this.campoSenha.text == this.campoConfirmaSenha.text;
			return (this.campoNome.text.length >= 3) && this.validarEmail (this.campoEmail.text) && senha;
		}
		
		private function onClickCadastrar(e:MouseEvent):void {
			var usuarioVO:UsuarioVO = new UsuarioVO ();
			usuarioVO.nome = this.campoNome.text;
			usuarioVO.email = this.campoEmail.text;
			usuarioVO.senha = this.campoSenha.text;
			
			var evento:TelaCadastroEvent;
			if (this.validarCadastro ()) {
				evento = new TelaCadastroEvent (TelaCadastroEvent.CADASTRAR, usuarioVO);
			} else { 
				evento = new TelaCadastroEvent (TelaCadastroEvent.CADASTRO_INVALIDO, usuarioVO);
			}
			this.dispatchEvent (evento);
		}
		
	}

}